package com.company.planner.entity;

import com.haulmont.chile.core.annotations.NamePattern;
import com.haulmont.cuba.core.entity.StandardEntity;
import com.haulmont.cuba.core.entity.annotation.OnDelete;
import com.haulmont.cuba.core.entity.annotation.OnDeleteInverse;
import com.haulmont.cuba.core.global.DeletePolicy;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import java.util.List;

@Table(name = "PLANNER_ES_CLUSTER_CONFIG")
@Entity(name = "planner_EsClusterConfig")
@NamePattern("%s %s|clusterName,nodeName")
public class EsClusterConfig extends StandardEntity {
    private static final long serialVersionUID = 1691442453952441268L;

    @Column(name = "CLUSTER_UUID")
    private String clusterUuid;

    @Column(name = "CLUSTER_NAME")
    private String clusterName;

    @Column(name = "CLUSTER_TITLE")
    private String clusterTitle;

    @Column(name = "MASTER_NODE")
    private String masterNode;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "PARENT_CLUSTER_CONFIG_ID")
    @OnDeleteInverse(DeletePolicy.CASCADE)
    private EsClusterConfig parentClusterConfig;

    @Column(name = "NODE_IP", nullable = false)
    @NotNull(message = "{msg://planner_EsClusterConfig.ip.validation.NotNull}")
    @Pattern(message = "{msg://planner_EsClusterConfig.ip.validation.Pattern}", regexp = "((25[0-5]|2[0-4]\\d|((1\\d{2})|([1-9]?\\d)))\\.){3}(25[0-5]|2[0-4]\\d|((1\\d{2})|([1-9]?\\d)))")
    private String nodeIp;

    @Column(name = "NODE_PORT")
    private Integer nodePort;

    @Column(name = "NODE_TYPE")
    private String nodeType;

    @Column(name = "NODE_STATE")
    private Integer nodeState;

    @Column(name = "NODE_ID")
    private String nodeId;

    @Column(name = "NODE_NAME")
    private String nodeName;

    @Column(name = "NODE_EPHEMERAL_ID")
    private String nodeEphemeralId;

    @Column(name = "NODE_TRANSPORT_ADDRESS")
    private String nodeTransportAddress;

    @Column(name = "NODE_HTTP_ADDRESS")
    private String nodeHttpAddress;

    @JoinTable(name = "PLANNER_INDEXS_CLUSTER_LINK",
            joinColumns = @JoinColumn(name = "ES_CLUSTER_CONFIG_ID"),
            inverseJoinColumns = @JoinColumn(name = "INDEXS_ID"))
    @ManyToMany
    @OnDelete(DeletePolicy.CASCADE)
    private List<Indexs> indexses;

    @Column(name = "ACTIVE")
    private Integer active;

    public ActiveEnum getActive() {
        return active == null ? null : ActiveEnum.fromId(active);
    }

    public void setActive(ActiveEnum active) {
        this.active = active == null ? null : active.getId();
    }

    public List<Indexs> getIndexses() {
        return indexses;
    }

    public void setIndexses(List<Indexs> indexses) {
        this.indexses = indexses;
    }

    public String getNodeHttpAddress() {
        return nodeHttpAddress;
    }

    public void setNodeHttpAddress(String nodeHttpAddress) {
        this.nodeHttpAddress = nodeHttpAddress;
    }

    public EsClusterConfig getParentClusterConfig() {
        return parentClusterConfig;
    }

    public void setParentClusterConfig(EsClusterConfig parentClusterConfig) {
        this.parentClusterConfig = parentClusterConfig;
    }

    public String getNodeTransportAddress() {
        return nodeTransportAddress;
    }

    public void setNodeTransportAddress(String nodeTransportAddress) {
        this.nodeTransportAddress = nodeTransportAddress;
    }

    public String getNodeEphemeralId() {
        return nodeEphemeralId;
    }

    public void setNodeEphemeralId(String nodeEphemeralId) {
        this.nodeEphemeralId = nodeEphemeralId;
    }

    public String getNodeId() {
        return nodeId;
    }

    public void setNodeId(String nodeId) {
        this.nodeId = nodeId;
    }

    public String getNodeName() {
        return nodeName;
    }

    public void setNodeName(String nodeName) {
        this.nodeName = nodeName;
    }

    public NodeState getNodeState() {
        return nodeState == null ? null : NodeState.fromId(nodeState);
    }

    public void setNodeState(NodeState nodeState) {
        this.nodeState = nodeState == null ? null : nodeState.getId();
    }

    public ClusterType getNodeType() {
        return nodeType == null ? null : ClusterType.fromId(nodeType);
    }

    public void setNodeType(ClusterType nodeType) {
        this.nodeType = nodeType == null ? null : nodeType.getId();
    }

    public Integer getNodePort() {
        return nodePort;
    }

    public void setNodePort(Integer nodePort) {
        this.nodePort = nodePort;
    }

    public String getNodeIp() {
        return nodeIp;
    }

    public void setNodeIp(String nodeIp) {
        this.nodeIp = nodeIp;
    }

    public String getClusterTitle() {
        return clusterTitle;
    }

    public void setClusterTitle(String clusterTitle) {
        this.clusterTitle = clusterTitle;
    }

    public String getMasterNode() {
        return masterNode;
    }

    public void setMasterNode(String masterNode) {
        this.masterNode = masterNode;
    }

    public String getClusterUuid() {
        return clusterUuid;
    }

    public void setClusterUuid(String clusterUuid) {
        this.clusterUuid = clusterUuid;
    }

    public String getClusterName() {
        return clusterName;
    }

    public void setClusterName(String clusterName) {
        this.clusterName = clusterName;
    }
}